package com.aibaixun.system.service.impl;

import com.aibaixun.common.core.domain.R;
import com.aibaixun.common.utils.BeanCopyUtils;
import com.aibaixun.common.utils.StringUtils;
import com.aibaixun.system.domain.TpTypeData;
import com.aibaixun.system.domain.TpTypeField;
import com.aibaixun.system.domain.dto.TpTypeDataPageDto;
import com.aibaixun.system.domain.dto.TpTypeFieldBatchDto;
import com.aibaixun.system.domain.dto.TpTypeFieldPageDto;
import com.aibaixun.system.domain.vo.TpTypeListVo;
import com.aibaixun.system.service.TpTypeDataService;
import com.aibaixun.system.service.TpTypeFieldService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.aibaixun.system.domain.TpType;
import com.aibaixun.system.service.TpTypeService;
import com.aibaixun.system.mapper.TpTypeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Comparator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
* @author zhangyh
* @description 针对表【tp_type(拓扑类型信息表)】的数据库操作Service实现
* @createDate 2024-06-12 15:08:43
*/
@Service
public class TpTypeServiceImpl extends ServiceImpl<TpTypeMapper, TpType>
    implements TpTypeService{

    @Autowired
    private TpTypeDataService tpTypeDataService;

    @Autowired
    private TpTypeFieldService tpTypeFieldService;

    @Override
    @Transactional
    public R saveTypeFieldBatch(TpTypeFieldBatchDto dto) {
        if(dto.getTypeId() == null){
            return R.fail("类别ID不能为空");
        }

        tpTypeFieldService.getBaseMapper().delete(
            new LambdaQueryWrapper<TpTypeField>()
            .eq(TpTypeField::getTypeId,dto.getTypeId())
        );
        List<TpTypeField> fieldList = dto.getFieldList();
        for (TpTypeField tpTypeField : fieldList) {
            tpTypeField.setFieldId(null);
            tpTypeField.setTypeId(dto.getTypeId());
        }
        tpTypeFieldService.saveBatch(fieldList);
        return R.ok();
    }

    @Override
    public R saveType(TpType dto) {
        if(StringUtils.isBlank(dto.getTypeName())){
            return R.fail("类别名称不能为空");
        }
        Long count = this.baseMapper.selectCount(
            new LambdaQueryWrapper<TpType>()
            .eq(TpType::getTypeName,dto.getTypeName())
        );
        if(count > 0){
            return R.fail("类别名称已存在");
        }
        this.baseMapper.insert(dto);

        return R.ok();
    }

    @Override
    public R updateType(TpType dto) {
        if(dto.getTypeId() == null){
            return R.fail("id不能为空");
        }
        TpType old = this.baseMapper.selectById(dto.getTypeId());
        if(old == null){
            return R.fail("类别信息异常");
        }
        if(StringUtils.isNotBlank(dto.getTypeName())
            && !StringUtils.equals(dto.getTypeName(),old.getTypeName())){
            Long count = this.baseMapper.selectCount(
                new LambdaQueryWrapper<TpType>()
                    .eq(TpType::getTypeName,dto.getTypeName())
            );
            if(count > 0){
                return R.fail("类别名称已存在");
            }
        }
        BeanCopyUtils.copy(dto,old);
        this.baseMapper.updateById(dto);

        return R.ok();
    }

    @Override
    @Transactional
    public R deleteType(Long typeId) {
        TpType old = this.baseMapper.selectById(typeId);
        if(old == null){
            return R.fail("类别信息异常");
        }
        this.baseMapper.deleteById(typeId);

        tpTypeFieldService.getBaseMapper().delete(
            new LambdaQueryWrapper<TpTypeField>()
            .eq(TpTypeField::getTypeId,typeId)
        );
        tpTypeDataService.getBaseMapper().delete(
            new LambdaQueryWrapper<TpTypeData>()
                .eq(TpTypeData::getTypeId,typeId)
        );

        return R.ok();
    }

    @Override
    public R<List<TpTypeListVo>> listType(TpType dto) {
        List<TpTypeListVo> resultList = new LinkedList<>();
        List<TpType> dataList = this.baseMapper.selectList(new LambdaQueryWrapper<>(dto));
        if(dataList.size() == 0){
            return R.ok(resultList);
        }
        for (TpType tpType : dataList) {
            Long count = tpTypeDataService.getBaseMapper().selectCount(
                new LambdaQueryWrapper<TpTypeData>()
                .eq(TpTypeData::getTypeId,tpType.getTypeId())
            );
            tpType.setDataCount(count.intValue());
        }

        Map<String,List<TpType>> dataMap = dataList.stream().collect(Collectors.groupingBy(TpType::getTypeField));
        for (String s : dataMap.keySet()) {
            TpTypeListVo listVo = new TpTypeListVo();
            List<TpType> data = dataMap.get(s);
            listVo.setTypeField(s);
            listVo.setTypeCount(data.size());
            listVo.setTypeData(data);
            resultList.add(listVo);
        }

        return R.ok(resultList);
    }

    @Override
    public R saveTypeField(TpTypeField dto) {
        if(StringUtils.isBlank(dto.getFieldName())){
            return R.fail("字段名称不能为空");
        }
        Long count = tpTypeFieldService.getBaseMapper().selectCount(
            new LambdaQueryWrapper<TpTypeField>()
                .eq(TpTypeField::getFieldName,dto.getFieldName())
        );
        if(count > 0){
            return R.fail("字段名称已存在");
        }
        tpTypeFieldService.getBaseMapper().insert(dto);

        return R.ok();
    }

    @Override
    public R updateTypeField(TpTypeField dto) {
        if(dto.getFieldId() == null){
            return R.fail("id不能为空");
        }
        TpTypeField old = tpTypeFieldService.getBaseMapper().selectById(dto.getTypeId());
        if(old == null){
            return R.fail("字段信息异常");
        }
        if(StringUtils.isNotBlank(dto.getFieldName())
            && !StringUtils.equals(dto.getFieldName(),old.getFieldName())){
            Long count = tpTypeFieldService.getBaseMapper().selectCount(
                new LambdaQueryWrapper<TpTypeField>()
                    .eq(TpTypeField::getFieldName,dto.getFieldName())
            );
            if(count > 0){
                return R.fail("字段名称已存在");
            }
        }
        BeanCopyUtils.copy(dto,old);
        tpTypeFieldService.getBaseMapper().updateById(dto);

        return R.ok();
    }

    @Override
    public R deleteTypeField(Long fieldId) {
        TpTypeField old = tpTypeFieldService.getBaseMapper().selectById(fieldId);
        if(old == null){
            return R.fail("字段信息异常");
        }
        tpTypeFieldService.getBaseMapper().deleteById(fieldId);
        return R.ok();
    }

    @Override
    public R<Page<TpTypeField>> getTypeFieldPage(Page page, TpTypeFieldPageDto pageDto) {
        TpTypeField dto = new TpTypeField();
        BeanCopyUtils.copy(pageDto,dto);
        Page<TpTypeField> resultPage = tpTypeFieldService.page(page,new LambdaQueryWrapper<>(dto));
        return R.ok(resultPage);
    }

    @Override
    public R saveTypeData(TpTypeData dto) {
        tpTypeDataService.getBaseMapper().insert(dto);
        return R.ok();
    }

    @Override
    public R updateTypeData(TpTypeData dto) {
        if(dto.getDataId() == null){
            return R.fail("数据信息异常");
        }
        TpTypeData old = tpTypeDataService.getBaseMapper().selectById(dto.getDataId());
        BeanCopyUtils.copy(dto,old);
        tpTypeDataService.getBaseMapper().updateById(old);
        return R.ok();
    }

    @Override
    public R deleteTypeData(Long dataId) {
        TpTypeData old = tpTypeDataService.getBaseMapper().selectById(dataId);
        if(old == null){
            return R.fail("数据信息异常");
        }
        tpTypeDataService.getBaseMapper().deleteById(dataId);
        return R.ok();
    }

    @Override
    public R<Page<TpTypeData>> getTypeDataPage(Page page, TpTypeDataPageDto pageDto) {
        TpTypeData dto = new TpTypeData();
        BeanCopyUtils.copy(pageDto,dto);
        Page<TpTypeData> resultPage = tpTypeDataService.page(page,new LambdaQueryWrapper<>(dto));
        return R.ok(resultPage);
    }

    @Override
    public R getAreaList() {
        List<String> resultList = new LinkedList<>();
        List<TpType> typeList = this.baseMapper.selectList(new LambdaQueryWrapper<>());
        if(typeList.size() == 0){
            return R.ok(resultList);
        }
        resultList = typeList.stream().map(TpType::getTypeField).collect(Collectors.toList());
        resultList = resultList.stream().distinct().collect(Collectors.toList());
        return R.ok(resultList);
    }
}




